<?php 
namespace app\api\controller\v1;

use think\Controller;

class News extends Controller
{
    public function read()
    {
        $id = input('id');
        $model = model('News');
        $data = $model->getNews($id);// 查询数据
        if ($data) {
            $code = 200;
        } else {
            $code = 404;
        }
        $data = [
            'code' => $code,
            'data' => $data
        ];
        return json($data);
    }
    public function del()
    {   
        $id = input('id');
        $model = model('News');
        $data = $model->del($id);//删除数据
        if ($data) {
            $code = 200;
        } else {
            $code = 404;
        }
        $data = [
            'code' => $code,
            'data' => $data
        ];
        return json($data);
    }
    public function reader()
    { 
        $requests  = input('post.');
        $size = input('size') ? input('size') : 20;
        $model = model('News');
        $data = $model->reader($size);// 查询数据
        if ($data) {
            $code = 200;
        } else {
            $code = 404;
        }
        $data = [
            'code' => $code,
            'data' => $data
        ];
        return json($data);
    }
    public function add()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        $shuju = input('post.');
        // $shuju =['id' => 18, 'title' => 'thinkphp', 'content' => 'thinkphp'];
        $model = model('News');
        $data = $model->add($shuju);// 添加数据
        if ($data) {
            $code = 200;
        } else {
            $code = 404;
        }
        $data = [
            'code' => $code,
            'data' => $data
        ];
        return json($data);
    }
    public function edit()
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        $shuju = input('post.');
        $id = input('id');
        $model = model('News');
        $data = $model->edit($id,$shuju);// 添加数据
        if ($data) {
            $code = 200;
        } else {
            $code = 404;
        }
        $data = [
            'code' => $code,
            'data' => $data
        ];
        return json($data);
    }
    // 图片上传
    public function image()
    {
         header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET, POST, PUT');
        // //保存的相对于uploads的子目录
        // $subPath = 'uploads' .DS. 'images';
        // //保存路径
        // $moveToPath = ROOT_PATH . 'public' .DS. $subPath;
        // //获取上传文件
        // $file = request()->file('image');

        // //图片验证规则
        // $rule = [
        //     'ext' => 'jpg,png,gif,jpeg',
        // ];

        // $result = $file->move($moveToPath);

        // if($result) {
        //     //文件后缀
        //     $saveExt  = $result->getExtension();
        //     //文件路径
        //     $savePath = $result->getSaveName();
        //     //文件名
        //     $saveName = $result->getFilename();

        //     $saveUrl = DS.$subPath.DS.$savePath;
        //     // $response = [
        //     //     'save_url' => $saveUrl,
        //     //     'show_url' => Util::getImageUrl($saveUrl),
        //     // ];
        //     return json(['code'=>1,'msg'=>'success','data'=>$response]);

        // }else{
        //     // 上传失败获取错误信息
        //     return json(['code'=>0,'msg'=>$file->getError(),'data'=>(object)[]]);
        // }
        $file=request()->file('image');             
if(!empty($file)){
    $info = $file->validate(['size'=>15678000,'ext'=>'jpg,png,gif'])->move( 'uploads');
    if($info){
        
       $data['img']="\uploads\\".$info->getSaveName();
           return json(['code'=>1,'msg'=>'success','data'=>$data['img']]);                   
    }else{             
        return json(['code'=>0,'msg'=>$file->getError()]);                  
    }
}
    }
}
